package com.sdgakj.utils.redissession.service.impl;

import com.sdgakj.utils.RedisClient;
import com.sdgakj.utils.SerializeUtils;
import com.sdgakj.utils.redissession.service.ShiroSessionRepository;
import com.sdgakj.utils.redissession.session.ShiroSession;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.session.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.Serializable;
import java.util.Collection;

public class ShiroSessionRepositoryImpl implements ShiroSessionRepository {

    private static Logger logger = LoggerFactory.getLogger(ShiroSessionRepositoryImpl.class);
    private static final String REDIS_SHIRO_SESSION = "shiro-session:";
    private static final int SESSION_VAL_TIME_SPAN = 1800;
    
    // 保存到Redis中key的前缀 prefix+sessionId
    private String redisShiroSessionPrefix = REDIS_SHIRO_SESSION;

    // 设置会话的过期时间
    private int redisShiroSessionTimeout = SESSION_VAL_TIME_SPAN;

    @Override
    public void saveSession(Session session) {
        if (session == null || session.getId() == null)
            throw new NullPointerException("session is empty");
        try {
            RedisClient.cacheAdd(buildRedisSessionKey(session.getId()), SerializeUtils.serializeToString((ShiroSession) session), redisShiroSessionTimeout);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    @Override
    public void updateSession(Session session) {
        if (session == null || session.getId() == null)
            throw new NullPointerException("session is empty");
        try {
	
			RedisClient.cacheAdd(buildRedisSessionKey(session.getId()), SerializeUtils.serializeToString((Serializable) session),
                    redisShiroSessionTimeout);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void refreshSession(Serializable sessionId) {
        RedisClient.cacheExpire(buildRedisSessionKey(sessionId), redisShiroSessionTimeout);
    }

    @Override
    public void deleteSession(Serializable id) {
        if (id == null) {
            throw new NullPointerException("session id is empty");
        }
        try {
            String key = buildRedisSessionKey(id);
            String value = RedisClient.cacheGet(key);
            if(StringUtils.isNotBlank(value))
            {
                RedisClient.cacheDel(key);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public Session getSession(Serializable id) {
        Session session = null;
        try {
            String key = buildRedisSessionKey(id);  
            String value = RedisClient.cacheGet(key);
            if (StringUtils.isNotBlank(value)) {  
                session = SerializeUtils.deserializeFromString(value);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return session;
    }

    @Override
    public Collection<Session> getAllSessions() {
        return null;
    }

    private String buildRedisSessionKey(Serializable sessionId) {
        return redisShiroSessionPrefix + sessionId;
    }

    public void setRedisShiroSessionPrefix(String redisShiroSessionPrefix)
    {
        this.redisShiroSessionPrefix = redisShiroSessionPrefix;
    }
    
    public void setRedisShiroSessionTimeout(int redisShiroSessionTimeout)
    {
        this.redisShiroSessionTimeout = redisShiroSessionTimeout;
    }
}